﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Data;
using ClassLibrary.Entitty;


public partial class Admin_ucControl_ucInsertInfo : System.Web.UI.UserControl
{
    TblMenuDAO menuDAO = new TblMenuDAO(Global.CONNECTION_STRING);
    TblMenuItemDAO menuItemDAO = new TblMenuItemDAO(Global.CONNECTION_STRING);
    TblInforDAO infoDAO = new TblInforDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string id = Request.QueryString["id"];
            LoadMenu();
            if (id == null || id == "")
            {
                LoadMenu();
            }
            else
            {
                LoadDetail();
            }     
          
        }
    }

    //load menu con
    private void LoadMenuItem()
    {
        drMenuItem.DataSource = menuItemDAO.GetMenuItem_All();
        drMenuItem.DataTextField = "MenuItemName";
        drMenuItem.DataValueField = "ID_MenuItem";
        drMenuItem.DataBind();
        ListItem lstItem = new ListItem("- Select -", "");
        drMenuItem.Items.Insert(0, lstItem);
    }

    //load_Menu
    private void LoadMenu()
    {
        drmenu.DataSource = menuDAO.GetAll_Admin();
        drmenu.DataTextField = "MenuName";
        drmenu.DataValueField = "ID";
        drmenu.DataBind();
        ListItem lstItem = new ListItem("- Select -", "");
        drmenu.Items.Insert(0, lstItem);
    }

    // load Detail
    private void LoadDetail()
    {
        LoadMenu();
        LoadMenuItem();
        string id = Request.QueryString["id"];
        TblInformaton info = infoDAO.GetInfor_ByIdInfo(Global.ConvertToInt(id));
        txtInforTitle.Text = info.Info_Title;
        editInfo.Value = info.Information;
        drLang.SelectedValue = info.IdLang.ToString();
        drMenuItem.SelectedValue = info.ID_MenuItem.ToString();
        drmenu.SelectedValue = info.ID.ToString();

    }

    // Load_MenuItem
    protected void drmenu_SelectedIndexChanged(object sender, EventArgs e)
    {
        drMenuItem.DataSource = menuItemDAO.GetMenuItem_ByIDMenu(Global.ConvertToInt(drmenu.SelectedValue));
        drMenuItem.DataTextField = "MenuItemName";
        drMenuItem.DataValueField = "ID_MenuItem";
        drMenuItem.DataBind();
    }

    protected void btAdd_Click(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        TblInformaton information = new TblInformaton();
        bool flag = false;

        information.Info_Title = txtInforTitle.Text;
        information.ID =Convert.ToInt32 (drmenu.SelectedValue);
        information.ID_MenuItem = Convert.ToInt32(drMenuItem.SelectedValue);
        information.IdLang = Convert.ToInt32(drLang.SelectedValue);
        information.Information = Server.HtmlDecode(editInfo.Value);
        

        if (id == "" || id == null)
        {
           // information.ID = 0;
            infoDAO.Insert(information, 1);
            ScriptManager.RegisterStartupScript(btAdd, btAdd.GetType(), "alert", "alert('Thêm thành công');", true);           
           
        }
        else
        {
            information.Id_Info = Global.ConvertToInt(id);
            infoDAO.Insert(information, 2);
            ScriptManager.RegisterStartupScript(btAdd, btAdd.GetType(), "alert", "alert('Cập nhật thành công');", true);           
           
        }
        flag = true;
        if (flag)
        {
            Response.Redirect("~/Admin/Default.aspx");
        }
    }
   
}
